package ThreadJoinTest;

/**
 * ClassName TestDemo
 * Description TODO
 * Author Hexiao
 * Date 2018/8/24 15:10
 * Version 1.0
 **/
public class TestDemo {
    public static void main(String[] args) {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start.");
        BThread bt = new BThread();
        AThread at = new AThread(bt);
        try {
            bt.start();
            Thread.sleep(2000);
            at.start();
            at.join();//等到at结束了，此线程才能向下执行。
        } catch (Exception e) {
            System.out.println("Exception from main");
        }
        System.out.println(threadName + " end!");
    }
//    main start.//主线程起动，因为调用了at.join()，要等到at结束了，此线程才能向下执行。
//    BThread constructor.....
//    AThread constructor.....
//    Thread-0 start. //线程bt启动
//    Thread-0 loop at 0
//    Thread-0 loop at 1
//    Thread-0 loop at 2
//    Thread-1 start. //线程at启动，因为调用bt.join()，等到bt结束了才向下执行。
//    Thread-0 loop at 3
//    Thread-0 loop at 4
//    Thread-0 end. //线程bt运行结束,线程at在bt.join();阻塞处起动，向下继续执行的结果
//    Thread-1 loop at 0
//    Thread-1 loop at 1
//    Thread-1 loop at 2
//    Thread-1 loop at 3
//    Thread-1 loop at 4
//    Thread-1 end.//线程at运行结束
//    main end!//主线程结束
}